﻿/**
 * @author WangYi
 */
var objdivtest;//全局div选择器
var objtexttest;//全局文本框
function selectDict(txtId,strPid,divId)
{
	var buttons=document.getElementsByTagName("input");
	for(i=0;i<buttons.length;i++)
	{
		if(buttons[i].type=="button" && buttons[i].id!="btnCon" && buttons[i].id!="btnOk")
		{
			buttons[i].disabled=true;
		}
	}
	objdivtest = document.getElementById(divId);
	objtexttest = document.getElementById(txtId);
	if (objtexttest.value != "") 
	{
		objdivtest.style.display = "block";
	}
	else 
	{
		getdict(strPid);
	}
	objdivtest.style.display = "block";
}
function closeDiv()
{
	objdivtest.style.display="none";

	var buttons=document.getElementsByTagName("input");
	for(i=0;i<buttons.length;i++)
	{
		if(buttons[i].type=="button")
		{
			buttons[i].disabled=false;
		}
	}
}
function ok()
{
	objdivtest.style.display="none";
	var buttons=document.getElementsByTagName("input");
	//更新所选数据到文本域
	var divchildren =objdivtest.children;
	objtexttest.value="";
	for(i=0;i<divchildren.length;i++)
	{
		if (divchildren[i].name == "DictCheckBox") {
			if (divchildren[i].checked) {
				objtexttest.value += divchildren[i].id + ":" + divchildren[i].value + ",";
			}
		}
	}
	//恢复按钮保护
	for(i=0;i<buttons.length;i++)
	{
		if(buttons[i].type=="button")
		{
			buttons[i].disabled=false;
		}
	}	
}

function getdict(pid)
{ 
     DataForAjax.GetDictItemByPId(pid,OnRequestComplete);
}

function OnRequestComplete(result)
{
    insertDiv(result);
}


function insertDiv(strJson)
{
    var strHtml;
	strHtml= "请选择：";
	strHtml+="<hr>";
	strHtml+=CreateControls(strJson);
	strHtml+="<hr/><br/>";
	
	strHtml+= "<div style=\"position:absolute; right:50px; bottom:1px;\"><input type=\"button\" id=\"btnCon\" value=\"取消\" onClick=\"closeDiv();\" style=\"height:22px;\"></div>";
	strHtml+="<div style=\"position:absolute; right:2px; bottom:1px;\" ><input type=\"button\" id=\"btnOk\" value=\"确定\" onClick=\"ok();\" style=\"height:22px;\"></div>";
	objdivtest.innerHTML=strHtml;
	//选择器样式：
	objdivtest.style.padding="2px";
	objdivtest.style.position="absolute";
	objdivtest.style.left="300px";
	objdivtest.style.top="100px";
	objdivtest.style.backgroundColor="#cccccc";
	objdivtest.style.width="200px";
	objdivtest.style.border="#000000 double 1px";
	objdivtest.style.fontSize="12px";
}
function CreateControls(strJson)
{
    var controls=eval(strJson);
    var str="";
	var i=0;
   	while(controls[i]!= undefined)
	{
		str+="<lable><input type=\"checkBox\" id=\""+controls[i].DictId+"\" name=\"DictCheckBox\" value=\""+controls[i].DictName+"\">"+controls[i].DictName+"</lable><br>"
		i++;
	}
    return str;
}
    